package com.mz.ozy.widget.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.activity.me.MeRechargeActivity;
import com.mz.ozy.activity.me.PayPwdActivity;
import com.mz.ozy.bean.EventBean;
import com.mz.ozy.bean.VoucherBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.utils.AppUtils;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by administrator on 2019/3/28.
 * 支付选择弹窗
 */
public class PaySelectDialog extends BaseDialog implements View.OnClickListener {

    public static final int PAY_TYPE_BALANCE = 0;
    public static final int PAY_TYPE_WX = 2;
    public static final int PAY_TYPE_ALIPAY = 1;
    public static final int PAY_TYPE_JF = 3;

    public static final int POY_TYPE = 4;
    private TextView tv_money, tv_balance;
    private ImageView iv_balance_select, iv_wx_select, iv_alipay_select, iv_paypal_select, iv_jf_select;
    private LinearLayout ll_balance, ll_alipay, ll_wx, ll_jf;
    private int type = PAY_TYPE_ALIPAY;   //支付方式    1-余额支付  2-微信支付  3-支付宝支付 4-积分支付
    private int fromId;  //0-开通会员  1-充值   2-订单支付
    private String balace1;
    private String m;

    public PaySelectDialog(Activity mContext, int fromId) {
        super(mContext, R.layout.dialog_pay_select, Gravity.BOTTOM, true, true, R.style.dialog_pay_theme);
        this.fromId = fromId;
        initView();
    }

    private void initView() {
        tv_money = getView(R.id.tv_money);
        tv_balance = getView(R.id.tv_balance);
        iv_balance_select = getView(R.id.iv_balance_select);
        iv_wx_select = getView(R.id.iv_wx_select);
        iv_alipay_select = getView(R.id.iv_alipay_select);
        iv_jf_select = getView(R.id.iv_jf_select);
        iv_paypal_select = getView(R.id.iv_paypal_select);
        ll_balance = getView(R.id.ll_balance);
        ll_jf = getView(R.id.ll_jf);
        ll_alipay = getView(R.id.ll_alipay);
        ll_wx = getView(R.id.ll_wx);

        getView(R.id.rl_cancel).setOnClickListener(this);
        getView(R.id.tv_recharge).setOnClickListener(this);
        getView(R.id.ll_balance).setOnClickListener(this);
        ll_wx.setOnClickListener(this);
        ll_alipay.setOnClickListener(this);
        getView(R.id.tv_confirm).setOnClickListener(this);
        getView(R.id.ll_pay_pal).setOnClickListener(this);
        ll_jf.setOnClickListener(this);
    }


    public void setData(String money, String balance) {
        tv_money.setText("¥" + money);
        m = money;
        balace1 = balance;
        if (!TextUtils.isEmpty(balance)) {
            tv_balance.setText("¥" + Utils.decimalUtils(balance, 2));

        } else {
            ll_balance.setVisibility(View.GONE);
            type = PAY_TYPE_ALIPAY;
            updateSelectStatus();
        }


        //纯积分商品
        if (money != null && money.contains("积分") && !money.contains("+")) {
            ll_alipay.setVisibility(View.GONE);
            ll_wx.setVisibility(View.GONE);
            ll_jf.setVisibility(View.VISIBLE);
            type = PAY_TYPE_JF;
            updateSelectStatus();
        } else {


        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_recharge:
                isDialogCancel = false;
                MeRechargeActivity.go(mContext, balace1);
                toggleDialog();
                break;
            case R.id.ll_balance:
                type = PAY_TYPE_BALANCE;
                updateSelectStatus();
                break;
            case R.id.ll_wx:
                type = PAY_TYPE_WX;
                updateSelectStatus();
                break;
            case R.id.ll_alipay:
                type = PAY_TYPE_ALIPAY;
                updateSelectStatus();
                break;
            case R.id.ll_pay_pal:
                type = POY_TYPE;
                updateSelectStatus();
                break;
            case R.id.ll_jf:
                type = PAY_TYPE_JF;
                updateSelectStatus();
                break;
            case R.id.tv_confirm:
                isDialogCancel = false;
                handleConfirm();
                break;
            case R.id.rl_cancel:
                isDialogCancel = true;
                toggleDialog();
                break;
        }
    }

    private void updateSelectStatus() {
        iv_balance_select.setImageResource(R.mipmap.cb_red_normal);
        iv_wx_select.setImageResource(R.mipmap.cb_red_normal);
        iv_alipay_select.setImageResource(R.mipmap.cb_red_normal);
        iv_paypal_select.setImageResource(R.mipmap.cb_red_normal);
        iv_jf_select.setImageResource(R.mipmap.cb_red_normal);
        if (type == PAY_TYPE_BALANCE) {
            iv_balance_select.setImageResource(R.mipmap.cb_red_select);
        } else if (type == PAY_TYPE_WX) {
            iv_wx_select.setImageResource(R.mipmap.cb_red_select);
        } else if (type == PAY_TYPE_ALIPAY) {
            iv_alipay_select.setImageResource(R.mipmap.cb_red_select);
        } else if (type == PAY_TYPE_JF) {
            iv_jf_select.setImageResource(R.mipmap.cb_red_select);
        } else {
            iv_paypal_select.setImageResource(R.mipmap.cb_red_select);
        }
    }

    /**
     * 在此处进行简单判断，
     */
    private void handleConfirm() {
        if (SPUtils.getInstance(mContext).getInt("paynum", 0) == 0) {
            final BaseDialog dialog1 = new BaseDialog(mContext, R.layout.dialog_normal, Gravity.CENTER, true);
            ((TextView) dialog1.getView(R.id.tv_dialog_content)).setText("是否设置支付密码?");
            ((TextView) dialog1.getView(R.id.tv_dialog_title)).setText("还未设置支付密码");
            dialog1.getView(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog1.toggleDialog();
                }
            });
            dialog1.getView(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(new EventBean(Constants.EventCode.JUMP_PAY_PWD, null));
//                    mContext.startActivity(new Intent(mContext, PayPwdActivity.class));
                    dialog1.toggleDialog();
                }
            });
            dialog1.toggleDialog();
            return;
        }
        if (type == -1) {
            ToastUtils.showToast("请选择支付方式");
            return;
        }
        toggleDialog();
        switch (type) {
            case PAY_TYPE_BALANCE: //余额支付->显示支付密码弹窗
                showPayDialog();
                break;
            case PAY_TYPE_JF: //积分支付->显示支付密码弹窗
                showPayDialog();
                break;
            case PAY_TYPE_WX:  //微信支付->回调接口
                if (!AppUtils.checkWxInstalled(mContext)) {
                    ToastUtils.showToast("请先安装微信");
                    return;
                }
                mListener.onConfirm(type, "");
                break;
            case PAY_TYPE_ALIPAY:  //支付宝支付->回调接口
                if (!AppUtils.checkAliPayInstalled(mContext)) {
                    ToastUtils.showToast("请先安装支付宝");
                    return;
                }
                mListener.onConfirm(type, "");
                break;
        }
    }

    /**
     * 显示支付密码弹窗
     */
    private void showPayDialog() {
//        if (0 == Constants.isPayPwd) {
//            ToastUtils.showToast("请先设置支付密码");
//            ModifyPwdActivity.go(mContext, 1);
//            return;
//        }1
        isPayCancel = true;
        final PayPassDialog dialog = new PayPassDialog(mContext);
        if (type == PAY_TYPE_BALANCE) {
            dialog.getPayViewPass().setForgetText(m);
            dialog.getPayViewPass().setYeText(balace1);
        } else {
            dialog.getPayViewPass().setForgetText(m);
            dialog.getPayViewPass().setYeText("");
        }
        dialog.getPayViewPass()
                .setPayClickListener(new PayPassView.OnPayClickListener() {
                    @Override
                    public void onPassFinish(String passContent) {
                        //6位输入完成回调
                        isPayCancel = false;
                        mListener.onConfirm(type, passContent);
                        dialog.dismiss();
                    }

                    @Override
                    public void onPayClose() {
                        isPayCancel = true;
                        dialog.dismiss();
                        //关闭回调
                    }

                    @Override
                    public void onPayForget() {
                        isPayCancel = false;
                        dialog.dismiss();
                        //点击忘记密码回调
//                        mContext.startActivity(new Intent(mContext, PayPwdActivity.class));
                        EventBus.getDefault().post(new EventBean(Constants.EventCode.JUMP_PAY_PWD, null));

                    }
                });
        dialog.setDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                if (isPayCancel) {
                    if (mListener != null) {
                        mListener.onCancel();
                    }
                }
            }
        });
    }

    private boolean isDialogCancel = true;
    private boolean isPayCancel = true;
    private onDialogListener mListener;

    public void setOnDialogListener(onDialogListener listener) {
        mListener = listener;
        setDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                if (isDialogCancel) {
                    if (mListener != null) {
                        mListener.onCancel();
                    }
                }
            }
        });
    }

    public interface onDialogListener {

        /**
         * @param type   支付类型，此处只是dialog最判断用，不同接口支付类型对应的id可能不同
         * @param payPwd 支付密码
         */
        void onConfirm(int type, String payPwd);

        void onCancel();
    }
}
